package com.richapp.launcher.scene;

import android.annotation.SuppressLint;
import android.util.Log;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.jack.util.date.DateUtil;
import com.richapp.launcher.database.system.dao.ConditionSensorModelDao;
import com.richapp.launcher.database.system.dao.ConditionStatusModelDao;
import com.richapp.launcher.database.system.dao.SceneModelDao;
import com.richapp.launcher.database.system.mode.ConditionSensorModel;
import com.richapp.launcher.database.system.mode.SceneModel;
import com.richapp.launcher.scene.conditions.sensor.SceneSensorAirConditions;
import com.richapp.launcher.scene.conditions.sensor.SceneSensorAlarmConditions;
import com.richapp.launcher.scene.conditions.sensor.SceneSensorCo2Conditions;
import com.richapp.launcher.scene.conditions.sensor.SceneSensorLightConditions;
import com.richapp.launcher.scene.conditions.sensor.SceneSensorPm25Conditions;
import com.richapp.launcher.scene.conditions.sensor.SceneSensorTemperatureHumidityConditions;
import com.richapp.launcher.scene.conditions.status.SceneStatusCloseConditions;
import com.richapp.launcher.scene.conditions.status.SceneStatusDimmingConditions;
import com.richapp.launcher.scene.conditions.status.SceneStatusOpenConditions;
import com.richapp.launcher.scene.conditions.time.SceneTimeAgingConditions;
import com.richapp.launcher.scene.conditions.time.SceneTimeDelayConditions;
import com.richapp.launcher.scene.conditions.time.SceneTimeDimmingConditions;
import com.richapp.launcher.scene.interfaces.ISceneAction;
import com.richapp.launcher.scene.interfaces.ISceneConditions;

public class Scene {
	
	private static final String TAG = Scene.class.getName();
	private static Scene scene = null;
	/**
	 * 情景类型 0:主动触发情景，1:被动触发情景
	 */
	public static final int SCENE_TYPE_ACTIVE = 0;
	public static final int SCENE_TYPE_PASSIVE = 1;
	
	/**
	 * 情景条件检查模式
	 */
	public static final String SCENE_CONDITION_CHECK_MODEL_ONE = "false";
	public static final String SCENE_CONDITION_CHECK_MODEL_ALL = "true";

	/**
	 * 传感类判断时间间隔
	 */
	public static final long TIME_SENSOR_CHECK = 1000;

	
	//情景名
	private String sceneName = null;
	//开启状态
	private String openFlag = "true";
	//情景类型
	private int sceneType = 0;
	//情景检查模式
	private String checkModel = "true";
	//情景条件
	private List<ISceneConditions> conditionList = null;
	//情景动作
	private List<ISceneAction> actionList = null;

	//情景周期
	private String iStatusCycleFlag;

	//上次执行的时间记录
	private long lastCheckTime;
	
	@SuppressLint("SimpleDateFormat")
	public static final SimpleDateFormat SDF = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
	
	public Scene() {
		conditionList = new LinkedList<ISceneConditions>();
		actionList = new LinkedList<ISceneAction>();
	}

	public static Scene  getIntance(){
		if(scene == null){
			scene = new Scene();
		}
		return scene;
	}

	public String getSceneName() {
		if(sceneName == null){
			sceneName = "情景"+SDF.format(new Date());
		}
		return sceneName;
	}


	public void setSceneName(String sceneName) {
		this.sceneName = sceneName;
	}


	public int getSceneType() {
		return sceneType;
	}


	public String getOpenFlag() {
		return openFlag;
	}

	public void setOpenFlag(String openFlag) {
		this.openFlag = openFlag;
	}

	public void setSceneType(int sceneType) {
		this.sceneType = sceneType;
	}

	public String getiStatusCycleFlag() {
		return iStatusCycleFlag;
	}

	public void setiStatusCycleFlag(String iStatusCycleFlag) {
		this.iStatusCycleFlag = iStatusCycleFlag;
	}

	public String getCheckModel() {
		return checkModel;
	}

	public void setCheckModel(String checkModel) {
		this.checkModel = checkModel;
	}

	public List<ISceneConditions> getConditionList() {
		return conditionList;
	}


	public List<ISceneAction> getActionList() {
		return actionList;
	}

	public long getLastCheckTime() {
		return lastCheckTime;
	}

	public void setLastCheckTime(long lastCheckTime) {
		this.lastCheckTime = lastCheckTime;
	}

	public void checkCondition(){
		SceneModel sceneModel = new SceneModel();
		SceneModelDao sceneModelDao = new SceneModelDao(SceneServer.getInstance().getContext());
		boolean doFlag = false;
		long last = getLastCheckTime();
		long now = DateUtil.getDateLong();

		Log.d(getClass().getName()," now: " + now);
		Log.d(getClass().getName()," last: " + last);
		Log.d(getClass().getName()," TIME_SENSOR_CHECK: " + TIME_SENSOR_CHECK);
		Log.d(getClass().getName()," now - last: " + (now - last));
		if(Scene.SCENE_TYPE_PASSIVE == getSceneType()){
			//判断执行时间间隔
//			if(SCENE_CONDITION_CHECK_MODEL_ALL.equals(checkModel)){
//				Log.d(getClass().getName(),"isContainSensorConditions: "+ isContainSensorConditions());
//				if(isContainSensorConditions()){
//					if(now - last < TIME_SENSOR_CHECK){
//						return;
//					}
//				}
//			}else{
//				if(!isContainNotSensorConditions()){
//					if(now - last < TIME_SENSOR_CHECK){
//						return;
//					}
//				}
//			}

			//判断条件
			if(SCENE_CONDITION_CHECK_MODEL_ALL.equals(checkModel)){
				for(ISceneConditions condition : conditionList){
 					if((doFlag = condition.checkCondition(getSceneName()))){

					}else{
						break;
					}
				}
			}else{
				for(ISceneConditions condition : conditionList){
					if((doFlag = condition.checkCondition(getSceneName()))){

						break;
					}else{
						//do nothing
					}
				}
			}
			
		}else{
			doFlag = true;
		}
		
		if(doFlag){
				sceneModel = sceneModelDao.getSceneModelOnly(getSceneName());
				sceneModel.setStatusCycleFlag("false");
				sceneModelDao.add(sceneModel);
			//更新最后成功记录的时间
			setLastCheckTime(now);
			Log.d(TAG, "情景:"+getSceneName()+" 条件检测通过");
			new Thread(new Runnable() {
				@Override
				public void run() {
					for(ISceneAction action: actionList){
						action.doAction();
						action.doDelay();
					}
				}
			}).start();
			
		}
	}

	private boolean isContainSensorConditions(){
		boolean flag = false;
		if(conditionList != null){
			for(ISceneConditions condition : conditionList){
				if(condition instanceof SceneSensorAirConditions
						|| condition instanceof SceneSensorCo2Conditions
						|| condition instanceof SceneSensorLightConditions
						|| condition instanceof SceneSensorPm25Conditions
						|| condition instanceof SceneSensorTemperatureHumidityConditions){
					flag = true;
					break;
				}
			}
		}
		return flag;
	}

	private boolean isContainNotSensorConditions(){
		boolean flag = false;
		if(conditionList != null){
			for(ISceneConditions condition : conditionList){
				if(condition instanceof SceneSensorAlarmConditions
						|| condition instanceof SceneStatusCloseConditions
						|| condition instanceof SceneStatusDimmingConditions
						|| condition instanceof SceneStatusOpenConditions
						|| condition instanceof SceneTimeDelayConditions
						|| condition instanceof SceneTimeDimmingConditions
						|| condition instanceof SceneTimeAgingConditions){
					flag = true;
					break;
				}
			}
		}
		return flag;
	}

}
